<?php	
class game 
{
	var $numberOfMembers;
	var $cMember = array();
	var $maxWeeks;
	
	function game()	// This constructor loads all data from the game
	{	
		$cDatabase = new database();

		$connect = $cDatabase->connectToDatabase();
		$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
		$db = $cDatabase->selectDatabase($connect, $activeDatabase);

		$selectArray[0] = '*';
		$result = $cDatabase->selectFromDatabase($connect, 'memberlist', $selectArray, NULL, NULL, NULL, NULL);
		$finfo = mysqli_fetch_fields($result);

		$this->numberOfMembers = mysqli_num_rows($result);
		$this->maxWeeks = $cDatabase->GetMaxWeeks($connect);
		
		for ($i = 0; $i < mysqli_num_rows($result); $i++)	// Do each member's team at a time
		{
			$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
	
			// Get all the data for this member and put it in the class
			$this->cMember[$i] = new member;

			$error |= $this->cMember[$i]->setUsername($row['userName']);
			$error |= $this->cMember[$i]->setPassword($row['userPass']);
			$error |= $this->cMember[$i]->setMemberName($row['memberName']);
			$error |= $this->cMember[$i]->setTeamName($row['teamName']);
			$error |= $this->cMember[$i]->setMoneyPaid($row['moneyPaid']);
			$error |= $this->cMember[$i]->setActive($row['active']);
			$error |= $this->cMember[$i]->loadMemberPlayerInfo($row, $connect);
		}
		//$cDatabase->closeDatabase($connect);
	}
	
	function getNumOfMembers()
	{
		return $this->numberOfMembers;
	}
	
	function getMemberFromUsername($memberNumber, $loginName, $password)
	{
		//echo "<BR><BR>DB ".$this->cMember[$memberNumber]->getUsername()." ".$this->cMember[$memberNumber]->getPassword();
		//echo "<BR>THIS ".$loginName." ".$password;
		if (($loginName == $this->cMember[$memberNumber]->getUsername()) && ($password == $this->cMember[$memberNumber]->getPassword()))
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
	
	function calculatePositionsPerWeek()
	{
		$accumWeekScore = array();

		for ($week = 1; $week <= $this->maxWeeks; $week++)
		{	// Get the accumulative score for each member for each week and put into the array
			for ($i = 0; $i < $this->numberOfMembers; $i++)
			{
				if ($this->cMember[$i]->getScoreAtWeek($week) != NULL)
					$accumWeekScore[$i] = $this->cMember[$i]->getScoreAtWeek($week);
			}
	
			// Sort the scores for this week
			rsort($accumWeekScore);
	
			// Work out the position of each member for this week
			$flag = 0;
			for ($i = 0; $i < $this->numberOfMembers; $i++)
			{
				for ($j = 0; $j < $this->numberOfMembers; $j++)
				{	
					if ($this->cMember[$i]->getScoreAtWeek($week) == $accumWeekScore[$j])
					{	// We've found the score in the array
						//echo "<BR>week ".$week."member ".$cMember[$i]->getMemberName()." score ".$accumWeekScore[$i];
						if (!$flag)	// There's nobody on this score yet
							$this->cMember[$i]->setPosition(($j + 1), $week);	// Add one as this array starts at 0
						$flag++;
					}
				}
				$this->cMember[$i]->setMatchingScore($flag, $week);	// Indicates how many other members are on the same score
				$flag = 0;										// reset flag, it's a new member
			}
		}
		for ($i = 0; $i < $this->numberOfMembers; $i++)
		{
			for ($week = 1; $week <= sizeof($accumWeekScore); $week++)
			{
				$position[$week-1] = $this->cMember[$i]->getPosition($week);
			}
			
		}
	}
	
	function getMaxTeamValue($database)
	{
	}

}

?>